#!/usr/bin/ruby
$:.unshift File.join(File.dirname(__FILE__))
require 'AWS/SQS'
require 'basic_configuration'
require 'aws_context'
require 'pp'

# Dump the queue contents by repeatedly reading from it
#  until there is nothing left.

if ARGV[0] == '-?' || ARGV.size != 1 then
  puts "usage: dump-queue name"
  exit 1
end
name=ARGV[0]

Signal.trap("INT") do
  exit 2
end

config = BasicConfiguration.new
sqs = AwsContext.instance.sqs(*config.keys)
begin
  url = sqs.list_queues(name).first
  if url.nil? 
    puts "queue not found: #{name}"
    exit 1
  else  
    while true do
      res = sqs.receive_messages(url)
      if res.nil? || res.length == 0 then break end
      #pp res
      puts "#{res[0][:body]}"
      sqs.delete_message(url, res[0][:id])
    end
  end
rescue
  puts "error #{$!} #{name}"
  exit 1
end

